/**
 * @description API 接口，建议通过 utils/pinia.ts 中定义的 defineDataSource 使用，以获得更便捷的数据状态管理
 */
import type * as T from '@/api/type/agriculture.ts';
import { $ } from './_client';

// /**
//  * 获取浏览量数据   /api/page-views
//  */
// export async function getPageViews() {
//   const res = await $.get('/api/view-counts', {
//     params: {
//       populate: '*',
//     },
//   })
//   return res.data
// }
//
// /**
//  * 更新浏览量数据   /api/page-views/:id
//  * @param id - 浏览量记录的 ID
//  * @param newViews - 新的浏览量数值
//  */
// export async function updatePageViews(id: string, newViews: number) {
//   const res = await $.put(`/api/view-counts/${id}`, {
//     data: {
//       views: newViews,
//     },
//   })
//   return res.data
// }

/**
 *   获取首页导航栏图标图片   /api/icon
 */
export async function getHomeNavigationIcons() {
  const res = await $.get('/api/navigation-icons', {
    params: {
      populate: '*',
    },
  });
  return res.data;
}

/**
 *   获取首页轮播图图片   /api/home-carousels
 */
export async function getHomeCarousels() {
  const res = await $.get('/api/home-carousels', {
    params: {
      populate: '*',
    },
  });
  return res.data;
}

/**
 * 项目功能介绍数据   /api/project-functions
 */
export async function getProjectFunctionAPI() {
  const res = await $.get('/api/project-functions', {
    params: {
      populate: '*',
    },
  });
  return res.data;
}

/**
 * 项目介绍信息数据   /api/project-introductions
 */
export async function getProjectIntroductionAPI() {
  const res = await $.get('/api/project-introductions', {
    params: {
      populate: '*',
    },
  });
  return res.data;
}

/**
 * 项目动态数据   /api/project-dynamics
 */
export async function getProjectDynamic(req: T.pageItem) {
  const res = await $.get('/api/project-dynamics', {
    params: {
      'pagination[page]': req.pageNo,
      'pagination[pageSize]': req.pageSize,
      populate: '*',
    },
  });
  return res.data;
}

/**
 * 项目动态数据（通过ID查询）   /api/project-dynamics/${id}
 */
export async function getProjectDynamicById(id: string) {
  const res = await $.get(`/api/project-dynamics/${id}`, {
    params: {
      populate: '*',
    },
  });
  return res.data;
}

/**
 * 获取政策法规数据   /api/plant-quarantines
 */
export async function getPlantQuarantine(req: any): Promise<T.PolicyResponse> {
  const res = await $.get('/api/plant-quarantines', {
    params: {
      'pagination[page]': req.pageNo,
      'pagination[pageSize]': req.pageSize,
      sort: 'time:desc',
      populate: '*',
      filters: {
        $or: [{ name: { $contains: req.searchTerm } }, { type: { $contains: req.searchTerm } }],
      },
    },
  });
  return res.data as T.PolicyResponse;
}

/**
 * 入侵物种名录数据   /api/list-of-invasive-species
 */
export async function getInvasiveSpeciesList(req: T.pageItem) {
  const res = await $.get('/api/list-of-invasive-species', {
    params: {
      'pagination[page]': req.pageNo,
      'pagination[pageSize]': req.pageSize,
      sort: 'Time:desc',
      populate: '*',
      filters: {
        $or: [
          { ScientificName: { $contains: req.searchTerm } },
          { ChineseName: { $contains: req.searchTerm } },
        ],
      },
    },
  });
  return res.data;
}

/**
 * 获取入侵物种名录数据（通过ID查询）   /api/list-of-invasive-species/{id}
 */
export async function getInvasiveSpeciesById(id: string) {
  const res = await $.get(`/api/list-of-invasive-species/${id}`, {
    params: {
      populate: '*',
    },
  });
  return res.data;
}

/**
 * 行业动态数据   /api/industry-dynamics
 */
export async function getIndustryDynamic(req: T.pageItem) {
  const res = await $.get('/api/industry-dynamics', {
    params: {
      'pagination[page]': req.pageNo,
      'pagination[pageSize]': req.pageSize,
      sort: 'time:desc',
      populate: '*',
      filters: {
        title: { $contains: req.searchTerm },
      },
    },
  });
  return res.data;
}

/**
 * 获取行业动态数据（通过ID查询）   /api/industry-dynamics/${id}
 */
export async function getIndustryDynamicsById(id: string) {
  const res = await $.get(`/api/industry-dynamics/${id}`, {
    params: {
      populate: '*',
    },
  });
  return res.data;
}

/**
 * 培训课程数据   /api/training-courses
 */
export async function getTrainingCourse(req: T.pageItem) {
  const res = await $.get('/api/training-courses', {
    params: {
      'pagination[page]': req.pageNo,
      'pagination[pageSize]': req.pageSize,
      sort: 'time:desc',
      populate: '*',
      filters: {
        title: { $contains: req.searchTerm },
      },
    },
  });
  return res.data;
}

/**
 * 培训课程数据（通过ID查询）   /api/training-courses/${id}
 */
export async function getTrainingCourseById(id: string) {
  const res = await $.get(`/api/training-courses/${id}`, {
    params: {
      populate: '*',
    },
  });
  return res.data;
}

/**
 * 相关链接政府部门数据   /api/government-department-links
 */
export async function getGovernment(req: T.pageItem) {
  const res = await $.get('/api/government-department-links', {
    params: {
      'pagination[page]': req.pageNo,
      'pagination[pageSize]': req.pageSize,
      populate: '*',
    },
  });
  return res.data;
}

/**
 * 相关链接研究论文数据   /api/research-paper-links
 */
export async function getResearch(req: T.pageItem) {
  const res = await $.get('/api/research-paper-links', {
    params: {
      'pagination[page]': req.pageNo,
      'pagination[pageSize]': req.pageSize,
      populate: '*',
    },
  });
  return res.data;
}

/**
 * 成果展示数据   /api/results-displays
 */
export async function getResultDisplay(req: T.pageItem) {
  const res = await $.get('/api/results-displays', {
    params: {
      'pagination[page]': req.pageNo,
      'pagination[pageSize]': req.pageSize,
      sort: 'time:desc',
      populate: '*',
    },
  });
  return res.data;
}

/**
 * 成果展示数据（通过ID查询）   /api/results-displays/${id}
 */
export async function getResultDisplayById(id: string) {
  const res = await $.get(`/api/results-displays/${id}`, {
    params: {
      populate: '*',
    },
  });
  return res.data;
}

/**
 * 微影视图片数据   /api/microvideo-pictures
 */
export async function getMicroFileAPI(req: T.pageItem) {
  const res = await $.get('/api/microvideo-pictures', {
    params: {
      'pagination[page]': req.pageNo,
      'pagination[pageSize]': req.pageSize,
      sort: 'time:desc',
      populate: '*',
    },
  });
  return res.data;
}

/**
 * 微影视图片数据（通过ID查询）   /api/microvideo-pictures/${id}
 */
export async function getMicroFileById(id: string) {
  const res = await $.get(`/api/microvideo-pictures/${id}`, {
    params: {
      populate: '*',
    },
  });
  return res.data;
}
